package com.sjh.lib_app.adapter

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.IntDef
import androidx.recyclerview.widget.RecyclerView
import com.sjh.baseutils.loge
import com.sjh.lib_app.utils.dip

/**
 *  这是一个RecyclerView的分割线装饰器，
 *  这个分割线实现了两个效果
 *  1. 这个分割线可以左右留出距离
 *  2. 可以指定在顶部或者底部item添加圆角
 * @author : SunJH
 * @data : 2020/8/11
 */
class SpaceLineDecoration : RecyclerView.ItemDecoration() {
    private val paint = Paint()
    private var lineWidth = 0f
    private var linOrientation = RecyclerView.VERTICAL
    private var cornersType = normal
    private var changeBackgroundDrawable: Drawable? = null
    private var originBackgroundDrawable: Drawable? = null
    private var margin = 0f
    private var itemSpace = 0
    private var itemSpaceType = normal

    companion object {
        const val normal = 0            //不添加
        const val onelyFirst = 1          //只在顶部添加
        const val onelyLast = 2       //只在底部添加
        const val FirstAndLast = 3      //在顶部和底部添加
        const val all = 4               //所有的item都添加
    }

    @IntDef(onelyFirst, onelyLast, FirstAndLast, all)
    annotation class corners


    /**
     * 设置分割线属性，宽度和颜色
     */
    fun setSpaceLine(color: Int, lineWidth: Float): SpaceLineDecoration {
        paint.color = color
        this.lineWidth = lineWidth
        return this
    }

    /**
     * 设置给哪些item添加背景
     * 以及背景的drawable
     */
    fun setCornersType(
        @SpaceLineDecoration.corners cornersType: Int,
        changeBackgroundDrawable: Drawable,
        originBackgroundDrawable: Drawable
    ): SpaceLineDecoration {
        this.cornersType = cornersType
        this.changeBackgroundDrawable = changeBackgroundDrawable
        this.originBackgroundDrawable = originBackgroundDrawable
        return this
    }


    /**
     * 设置分隔线类型
     * HORIZONTAL 横线
     * VERTICAL    竖线
     */
    fun setlinOrientation(@RecyclerView.Orientation orientation: Int): SpaceLineDecoration {
        this.linOrientation = orientation
        return this
    }


    /**
     * 给分隔线设置间距 单位px
     * 如果设置了线的添加的是竖线，该margin会在上下生效
     * 如果设置了线的添加的是横线，该margin会在左右生效
     */
    fun setMargin(margin: Float): SpaceLineDecoration {
        this.margin = margin
        return this
    }

    /**
     * 给item设置间距，
     * @param itemSpace 间距，单位px
     * @param itemSpaceType 设置间距方式
     */
    fun setItemSpace(
        @SpaceLineDecoration.corners itemSpaceType: Int,
        itemSpace: Int
    ): SpaceLineDecoration {
        this.itemSpaceType = itemSpaceType
        this.itemSpace = itemSpace
        return this
    }

    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
        if (lineWidth == 0f) return
        val itemCount = parent.childCount
        for (index in itemCount - 1 downTo 1) {
            val view = parent.getChildAt(index)
            when (linOrientation) {
                RecyclerView.VERTICAL -> {
                    c.drawRect(
                        (view.left).toFloat(),
                        view.top + margin,
                        (view.left+lineWidth),
                        view.bottom - margin,
                        paint
                    )
                }
                RecyclerView.HORIZONTAL -> {
                    c.drawRect(
                        margin,
                        (view.top).toFloat(),
                        view.width.toFloat(),
                        view.top + lineWidth,
                        paint
                    )
                }
            }
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        val index = parent.getChildAdapterPosition(view)
        //添加item背景
        changeBackgroundDrawable?.let {
            when (cornersType) {
                normal -> {
                }
                onelyFirst -> {
                    if (index == 0) view.background = it else view.background =
                        originBackgroundDrawable
                }
                onelyLast -> {
                    if (index == state.itemCount - 1) view.background = it else view.background =
                        originBackgroundDrawable
                }
                FirstAndLast -> {
                    if (index == 0) view.background = it else view.background =
                        originBackgroundDrawable
                    if (index == state.itemCount - 1) view.background = it else view.background =
                        originBackgroundDrawable
                }
                all -> {
                    view.background = it
                }
            }
        }

        //给item添加间距
        if (itemSpace == 0 || itemSpaceType == normal) {
            return
        }
        if (linOrientation == RecyclerView.VERTICAL) {
            //添加的是竖线
            when (itemSpaceType) {
                normal -> {
                }
                onelyFirst -> {
                    if (index == 0) outRect.left += itemSpace
                }
                onelyLast -> {
                    if (index == state.itemCount - 1) outRect.right += itemSpace
                }
                FirstAndLast -> {
                    if (index == 0) outRect.left += itemSpace
                    if (index == state.itemCount - 1) outRect.right += itemSpace
                }
                all -> {
                    outRect.left += itemSpace
                    outRect.right += itemSpace
                }
            }
        } else {
            //添加的是横线
            when (itemSpaceType) {
                normal -> {
                }
                onelyFirst -> {
                    if (index == 0) outRect.top += itemSpace
                }
                onelyLast -> {
                    if (index == state.itemCount - 1) outRect.bottom += itemSpace
                }
                FirstAndLast -> {
                    if (index == 0) outRect.top += itemSpace
                    if (index == state.itemCount - 1) outRect.bottom += itemSpace
                }
                all -> {
                    outRect.top += itemSpace
                    outRect.bottom += itemSpace
                }
            }
        }
    }

}
